("use strict");
import customizedError from "./Error/customizedError.js";
import * as customizedErrorCode from "./Error/customizedErrorCode.js";
import submitFormView from "./View/submitFormView.js";
import submitStateView from "./View/submitStateView.js";
import * as model from "./model.js";

const controlSubmitForm = async function (rawForm) {
  try {
    // 提交表单
    await model.submitForm(rawForm);
    // 提示报名成功
    submitStateView.toggleSubmitStateWindow(model.state.form.name);
    // 清空表单
    submitFormView.clearForm();
  } catch (error) {
    submitFormView.renderError(error);
  }
};

const controlSubmitState = function () {
  if (model.state.form !== "") {
    submitStateView.toggleSubmitStateWindow(model.state.form.name);
  }
};

const controlGetSubmitForm = function () {
  return model.state.form;
};

const init = function () {
  submitFormView.addHandlerSubmit(controlSubmitForm);
  submitStateView.addHandlerCloseModal();
  submitStateView.addHandlerShowWindow(controlSubmitState);
  submitStateView.addHandlerUploadForm(controlGetSubmitForm);
};
init();
